package com.turing.post.thirdinterface.impl;

import java.io.Serializable;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;

import com.turing.post.thirdinterface.util.ThirdInterfaceApplicationException;
import com.turing.post.thirdinterface.util.ThirdInterfaceRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 三环应答基类
 * @author ZXL
 *
 */
@SuppressWarnings("serial")
public class SanHuanRequest implements Serializable {
	/**
	 * 交易码
	 */
	public String tranCode = "";
	/**
	 * 主机受理时间
	 */
	public String tranTime = "";
	/**
	 * 响应码
	 */
	public String respCode = "";
	/**
	 * 错误描述
	 */
	public String respErrMsg = "";
	
	/**
	 * 车牌号
	 */
	public String autoNumber = "";
	
	/**
	 * 气瓶编号
	 */
	public String gasCylinderNo = "";
	
	/**
	 * 气瓶数量
	 */
	public String gasCylinderNum = "";
	
	/**
	 * 注册编号
	 */
	public String regNo = "";
	
	/**
	 * 车辆种类
	 */
	public String autoType = "";
	
	/**
	 * 到检日期
	 */
	public String expiredDate = "";
	/**
	 * 解析XML开卡验证(销户)应答报文
	 * 
	 * @param doc
	 */
	public static SanHuanRequest parseOpenCardValidateXml(byte[] ibuf) throws PCPSApplicationException {
		Document doc = SanHuanXmlHelper.getXmlDocumentFromBuffer(ibuf);
		Element root = doc.getRootElement();
		SanHuanRequest sanHuanRequest = new SanHuanRequest();
		if (!PCPSUtil.compareObject(root.getName(),
				SanHuanXmlHelper.XML_NODE_TRANSRESPONSE))
			throw new PCPSApplicationException(ThirdInterfaceRespCode.RC_PARAMETER, 
					"invalid XML Root Node Name[" + root.getName()+ "]!");
		sanHuanRequest.tranCode = SanHuanXmlHelper.getMandatoryString(root,
				SanHuanXmlHelper.XML_NODE_TRANCODE);
		sanHuanRequest.tranTime = SanHuanXmlHelper.getMandatoryString(root,
				SanHuanXmlHelper.XML_NODE_TRANTIME);
		sanHuanRequest.respCode = SanHuanXmlHelper.getMandatoryString(root,
				SanHuanXmlHelper.XML_NODE_RESPCODE);
		sanHuanRequest.respErrMsg = SanHuanXmlHelper.getOptionalString(root,
				SanHuanXmlHelper.XML_NODE_RESPERRMSG);
		if (!SanHuanXmlHelper.XML_SUCCESS.equals(sanHuanRequest.respCode))
			return sanHuanRequest;
		
		List<Element> elements = root.getChildren();
		for (Element element: elements){
			if (!PCPSUtil.compareObject(element.getName(),
					SanHuanXmlHelper.XML_NODE_RESPBODY))
				continue;
//				throw new PCPSApplicationException(ThirdInterfaceRespCode.RC_PARAMETER, 
//						"invalid XML Root Node Name[" + element.getName()+ "]!");
			sanHuanRequest.autoNumber = SanHuanXmlHelper.getMandatoryString(element,
					SanHuanXmlHelper.XML_NODE_AUTONUMBER);
			sanHuanRequest.gasCylinderNo = SanHuanXmlHelper.getMandatoryString(element,
					SanHuanXmlHelper.XML_NODE_GASCYLINDERNO);
			sanHuanRequest.gasCylinderNum = SanHuanXmlHelper.getMandatoryString(element,
					SanHuanXmlHelper.XML_NODE_GASCYLINDERNUM);
			sanHuanRequest.regNo = SanHuanXmlHelper.getMandatoryString(element,
					SanHuanXmlHelper.XML_NODE_REGNO);
			sanHuanRequest.autoType = SanHuanXmlHelper.getMandatoryString(element,
					SanHuanXmlHelper.XML_NODE_AUTOTYPE);
			sanHuanRequest.expiredDate = SanHuanXmlHelper.getMandatoryString(element,
					SanHuanXmlHelper.XML_NODE_EXPIREDDATE);
		}
		return sanHuanRequest;
	}
	
	/**
	 * 解析XML黑名单、解除黑名单、卡赎回、换卡应答报文
	 * 
	 * @param doc
	 */
	public static SanHuanRequest parseCurrencyXml(byte[] ibuf) throws PCPSApplicationException {
		Document doc = SanHuanXmlHelper.getXmlDocumentFromBuffer(ibuf);
		Element root = doc.getRootElement();
		SanHuanRequest sanHuanRequest = new SanHuanRequest();
		if (!PCPSUtil.compareObject(root.getName(),
				SanHuanXmlHelper.XML_NODE_TRANSRESPONSE))
			throw new PCPSApplicationException(ThirdInterfaceRespCode.RC_PARAMETER, 
					"invalid XML Root Node Name[" + root.getName()+ "]!");
		sanHuanRequest.tranCode = SanHuanXmlHelper.getMandatoryString(root,
				SanHuanXmlHelper.XML_NODE_TRANCODE);
		sanHuanRequest.tranTime = SanHuanXmlHelper.getMandatoryString(root,
				SanHuanXmlHelper.XML_NODE_TRANTIME);
		sanHuanRequest.respCode = SanHuanXmlHelper.getMandatoryString(root,
				SanHuanXmlHelper.XML_NODE_RESPCODE);
		sanHuanRequest.respErrMsg = SanHuanXmlHelper.getOptionalString(root,
				SanHuanXmlHelper.XML_NODE_RESPERRMSG);
		return sanHuanRequest;
	}
	
	
	public static SanHuanRequest checkRequestBusinessType(String tranCode,byte[] ibuf) throws PCPSApplicationException {
		//验证参数
		if (ibuf == null || ibuf.length <= 0){
			throw new ThirdInterfaceApplicationException(
					ThirdInterfaceApplicationException.ERRMSG_NULLPOINTER,
					ThirdInterfaceRespCode.RC_ARGS_NULL, "参数为空!");
		}
		//验证业务类型
		if (PCPSUtil.isEmpty(tranCode)){
			throw new ThirdInterfaceApplicationException(
					ThirdInterfaceApplicationException.ERRMSG_NULLPOINTER,
					ThirdInterfaceRespCode.RC_ARGS_NULL, "交易码为空!");
		}
		
		if (SanHuanXmlHelper.XML_OPENCARDVALIDATE.equals(tranCode)){
			return parseOpenCardValidateXml(ibuf);
		}
		
		if (SanHuanXmlHelper.XML_OPENCARDREGISTER.equals(tranCode) 
				|| SanHuanXmlHelper.XML_BLACKORDER.equals(tranCode)
				|| SanHuanXmlHelper.XML_REMOVEBLACKORDER.equals(tranCode)
				|| SanHuanXmlHelper.XML_CARDREDEMP.equals(tranCode)
				|| SanHuanXmlHelper.XML_CHANGECARD.equals(tranCode)){
			return parseCurrencyXml(ibuf);
		}
		return null;
	}
}
